package com.egao.api.controller;

import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Random;

/**
 * 车辆出车率 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-03-21
 */
@RestController
@RequestMapping("/api/test")
@Api(value = "TestController",tags = {"测试使用"})
public class TestController extends BaseController {

	/**
	 * 修改个人信息
	 */
	@ApiOperation(value = "接口的功能介绍show",notes = "提示接口使用者注意事项",httpMethod = "POST")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id",value = "用户ID",paramType = "path",dataType = "String",required = true),
			@ApiImplicitParam(name = "name",value = "用户名称",paramType = "form",dataType = "String",required = true)
	})
	@PostMapping("/show")
	public JsonResult show(User user, HttpServletRequest request) {
		System.out.println("1111======");
		return JsonResult.ok("show()");
	}

	public static void main(String[] args) {
		BCryptPasswordEncoder a = new BCryptPasswordEncoder();
		String asa = a.encode("ddd");
		System.out.println(asa);
		System.out.println(a.matches("ddd",asa));

		String s = String.valueOf(System.currentTimeMillis() + new Random().nextInt());

		try {
			MessageDigest messageDigest = MessageDigest.getInstance("md5");
			byte[] digest = messageDigest.digest(s.getBytes());


			String t = Base64.getEncoder().encodeToString(digest);//不使用Base64的话会出现乱码。因为new String默认编码可能不能完全包含上面这个字节数组
			//base64将每三个字节转成4个字节，这样高位就使用00补齐，这样最大也就是63，最小为0。一共只有64种情况，就不会出现乱码了。
			System.out.println(t);
			/**
			 * Java8之前的做法
			 */
//            BASE64Encoder encoder = new BASE64Encoder();
//            return encoder.encode(digest);
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException();
		}

	}

	/**
	 * token是否过期 这个方法在使用的
	 */
	@PostMapping("/tokenExpired")
	public JsonResult tokenExpired() {
		return JsonResult.ok("tokenExpired()");
	}

}
